# This file is part of RhythmToWeb.

# RhythmToWeb is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# RhythmToWeb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with RhythmToWeb. If not, see <http://www.gnu.org/licenses/>.

import rb
import rhythmdb
import urllib2
from urllib import quote
import gobject
import gconf
import gtk.glade

gconf_keys = {
	'interval': '/apps/rhythmbox/plugins/rhythmtoweb/interval',
	'url'     : '/apps/rhythmbox/plugins/rhythmtoweb/url'
}

class RhythmToWeb(rb.Plugin):
	
	shell = None
	enabled = False
	entry = None
	config_diag = None
	
	def __init__(self):
		rb.Plugin.__init__(self)
	
	def activate(self, shell):
		self.shell = shell
		self.enabled = True
		client = gconf.client_get_default()
		self.interval = client.get_int(gconf_keys['interval']) or 2
		self.url = client.get_string(gconf_keys['url']) or None
		gobject.timeout_add(self.interval * 1000, self.check_and_send)
	
	def deactivate(self, shell):
		self.shell = None
		self.enabled = False
		# Make a synchronous request when deactivating to make sure it reaches
		# the server. Otherwise, it can remain 'stuck' at the last song played.
		if self.url: urllib2.urlopen(self.url)
	
	def check_and_send(self):
		if not self.enabled: return False
		if not self.url:
			gobject.timeout_add(self.interval * 1000, self.check_and_send)
			return False
		db = self.shell.get_property('db')
		entry = self.shell.props.shell_player.get_playing_entry()
		if entry and entry != self.entry:
			self.entry = entry
			artist   = db.entry_get(entry, rhythmdb.PROP_ARTIST)
			title    = db.entry_get(entry, rhythmdb.PROP_TITLE)
			album    = db.entry_get(entry, rhythmdb.PROP_ALBUM)
			genre    = db.entry_get(entry, rhythmdb.PROP_GENRE)
			year     = db.entry_get(entry, rhythmdb.PROP_YEAR)
			duration = db.entry_get(entry, rhythmdb.PROP_DURATION)
			url = self.url
			if '?' not in url: url += '?'
			url += '&artist='    + quote(artist)
			url += '&title='    + quote(title)
			url += '&album='    + quote(album)
			url += '&genre='    + quote(genre)
			url += '&year='     + quote( str(year) )
			url += '&duration=' + quote( str(duration) )
			loader = rb.Loader()
			# We don't care about output from the url
			loader.get_url(url, lambda data: False)
		elif not entry and self.entry:
			loader = rb.Loader()
			loader.get_url(self.url, lambda data: False)
		gobject.timeout_add(self.interval * 1000, self.check_and_send)
		return False
	
	def create_configure_dialog(self, dialog=None):
		if self.config_diag: return
		glade_file = self.find_file('rhythmtoweb-config.glade')
		self.config_diag = gtk.glade.XML(glade_file)
		self.config_diag.signal_autoconnect({
			'cancel': self.diag_cancel,
			'ok': self.diag_ok
		})
		client = gconf.client_get_default()
		interval = client.get_int(gconf_keys['interval']) or 2
		url = client.get_string(gconf_keys['url']) or ''
		self.config_diag.get_widget('interval-entry').set_value(interval)
		self.config_diag.get_widget('url-entry').set_text(url)
		return self.config_diag.get_widget('window')
	
	def diag_cancel(self, widget=None, data=None):
		self.config_diag.get_widget('window').destroy()
		self.config_diag = None
	
	def diag_ok(self, widget=None, data=None):
		url = self.config_diag.get_widget('url-entry').get_text()
		interval = self.config_diag.get_widget('interval-entry').get_value_as_int()
		client = gconf.client_get_default()
		client.set_string(gconf_keys['url'], url)
		client.set_int(gconf_keys['interval'], interval)
		self.url = url
		self.interval = interval
		self.config_diag.get_widget('window').destroy()
		self.config_diag = None

